/**
 * @Title: CartController
 * @Package cn.com.yiqi.wsc.api.controller
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-林威
 * @date 2017/2/23 11:04
 * @version V1.0
 */
package cn.com.yiqi.wsc.api.controller;

import cn.com.yiqi.wsc.Ro.CartVO;
import cn.com.yiqi.wsc.api.controller.ro.CommonRO;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.entity.customer.Cart;
import cn.com.yiqi.wsc.entity.customer.Customer;
import cn.com.yiqi.wsc.service.customer.CustomerService;
import cn.com.yiqi.wsc.service.customer.impl.CartServiceImpl;
import cn.com.yiqi.wsc.service.customer.impl.CustomerImpl;
import cn.com.yiqi.wsc.service.file.dataobject.QiniuConfig;

import java.math.BigDecimal;
import java.util.List;

import cn.com.yiqi.wsc.service.order.impl.OrderServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 *@ClassName: CartController
 * Description: TODO
 * @author 会员系统组-林威
 * @date 2017/2/23 11:04
 *
 */
@ResponseBody
@RequestMapping("/api/cart")
@Controller
public class ApiCartController {

    @Autowired
    CustomerService customerService;

    @Autowired
    CartServiceImpl cartService;

    @Autowired
    OrderServiceImpl orderService;
    
    @Autowired
    QiniuConfig qiniuConfig;
    
    
    /**
     * 购物车商品列表
     * */
    @RequestMapping("/itemList")
    public Result itemList(@RequestBody CommonRO ro){
        Result re = customerService.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }

        List<CartVO> list = cartService.findAll(ro.getUserId());
        BigDecimal total = BigDecimal.ZERO;
        for (CartVO o:list){
            total = total.add(o.getPrice().multiply(new BigDecimal(o.getCount()+"")));
            o.setItemUrl(qiniuConfig.getURL()+"/"+o.getItemUrl());
        }
        re.putData("total",total);
        re.putData("list",list);
        return re;
    }

    /**
     * 购物车商品数量调整
     * */
    @RequestMapping("/adjustCount")
    public Result adjustCount(@RequestBody CommonRO ro){
        Result re = new Result();
        if(ro.getId() == null){
            return ResultUtil.fail(re,"购物车商品id不能为空");
        }
        if(ro.getCount()==null || ro.getCount() <=0 ){
            ro.setCount(1);
        }
        Integer newCount = cartService.adjustCount(ro.getId(),ro.getCount());
        re.putData("newCount",newCount);
        re.putData("total",cartService.calCartTOtal(ro.getUserId()));
        return re;
    }

    /**
     * 加入购物车
     * */
    @RequestMapping("/addToCart")
    public Result addToCart(@RequestBody CommonRO ro ){
        Result re = customerService.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        cartService.addToCart(ro.getUserId(),ro.getItemId());
        return re;
    }

    /**
     * 从购物车中删除
     * */
    @RequestMapping("/deleteFromCart")
    public Result deleteFromCart(@RequestBody CommonRO ro ){
        Result re = customerService.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        cartService.deleteFromCart(ro.getId(),ro.getUserId());
        re.putData("total",cartService.calCartTOtal(ro.getUserId()));
        return re;
    }

    /**
     * 清空购物车
     * */
    @RequestMapping("/clearCart")
    public Result clearCart(@RequestBody CommonRO ro){
        Result re = customerService.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        cartService.clearCart(ro.getUserId());
        return re;
    }

    /**
     * 购物车去结算
     *
     * */
    @RequestMapping("/calCart")
    public Result calCart(@RequestBody CommonRO ro){
        Result<Customer> re = customerService.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }

        re = cartService.calCart(ro.getUserId());
        return re;
    }

    /**
     * 购物车结算
     * 去付款
     * */
    @RequestMapping("/pay")
    public Result pay(@RequestBody CommonRO ro){
        //检查收货地址
        Result<Customer> re = customerService.isExistUserAndAddress(ro.getUserId(),ro.getAddressId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        //检查用户是否绑定手机号，未绑定手机号不能生产订单
        if(StringUtils.isEmpty(re.getReturnValue().getTel())){
            return ResultUtil.fail(re,"您还未绑定手机，不能购买商品");
        }
        //生成订单
        re = cartService.pay(ro.getUserId(),ro.getAddressId());
        return re;
    }

    @RequestMapping("/count")
    public Result cartCount(@RequestBody CommonRO ro){
        Result<Customer> re = customerService.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        re.putData("total",cartService.findAll(ro.getUserId()).size());
        return re;
    }

}
